﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EFFC.Net.Manager;
using EFFC.Net.Resource;
using EFFC.Net.Common;
using EFFC.Net.Parameters;
using EFFC.Net.Returns;
using EFFC.Net.BatchBuilder.Public;
using EFFC.Net.Logic;
using EFFC.Net.BatchBuilder.Parameters;
using EFFC.Net.BatchBuilder.Returns;
using EFFC.Net.Interface;

namespace EFFC.Net.BatchBuilder.BatchManage
{
    public abstract class BatchDefaultManager : BatchManager
    {
        string _domaintype = GlobalString.DomainType.GLOBAL;
        /// <summary>
        /// 本Manager所属的Domain，框架属性
        /// </summary>
        public string DomainType
        {
            get { return _domaintype; }
            set { _domaintype = value; }
        }

        public override QueryReturn Query<T>(Parameters.BatchManagerParameter p)
        {
            T t = default(T);
            try
            {
                t = BaseLogic.CreateInstance<T>(this.GetHashCode().ToString());
                return t.Query(p);
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex, _domaintype);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override bool Update<T>(Parameters.BatchManagerParameter p)
        {
            T t = default(T);
            try
            {
                t = BaseLogic.CreateInstance<T>(this.GetHashCode().ToString());
                return t.Update(p);
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex, _domaintype);
                return false;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override bool Delete<T>(Parameters.BatchManagerParameter p)
        {
            T t = default(T);
            try
            {
                t = BaseLogic.CreateInstance<T>(this.GetHashCode().ToString());
                return t.Delete(p);
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex, _domaintype);
                return false;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override bool Insert<T>(Parameters.BatchManagerParameter p)
        {
            T t = default(T);
            try
            {
                t = BaseLogic.CreateInstance<T>(this.GetHashCode().ToString());
                return t.Insert(p);
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex, _domaintype);
                return false;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override QueryByPageReturn QueryByPage<T>(Parameters.BatchManagerParameter p)
        {
            T t = default(T);
            try
            {
                t = BaseLogic.CreateInstance<T>(this.GetHashCode().ToString());
                QueryByPageReturn rtn = new QueryByPageReturn();

                DBOParameterCollection sqlparameter = new DBOParameterCollection();
                t.Dao.Open(BatchConfigString.DBConn);
                t.Dao.BeginTrans();
                //预执行sql
                string presql = t.QueryByPageSQL_Prepare(p, ref sqlparameter);
                if (presql != "")
                {
                    DBAP dbap = ADBAccess.GetDBParametersDefine();
                    dbap.SQL = presql;
                    if (sqlparameter.Count > 0)
                    {
                        dbap.SQL_Parameters = sqlparameter;
                    }
                    t.Dao.ExecuteNoQuery(dbap);
                }
                //查询sql
                sqlparameter.Clear();
                string sql = t.QueryByPageSQL_QueryByPage(p, ref sqlparameter);
                DBAPageP dbapp = ADBAccess.GetDBParamPageDefine();
                dbapp.Count_of_OnePage = BatchConfigString.PageSize;
                dbapp.CurrentPage = p.CurrentPage;
                dbapp.SQL = sql;
                if (sqlparameter.Count > 0)
                {
                    dbapp.SQL_Parameters = sqlparameter;
                }
                if (p.GetValue("OrderBy") != null)
                {
                    dbapp.OrderBy = p.GetValue("OrderBy").ToString();
                }

                t.Dao.StartPageByCondition(dbapp);
                rtn.DataResult = t.Dao.GoToPage(p.GoToPage);
                rtn.TotalCount = t.Dao.RowCountByPage;
                rtn.CurrentPage = t.Dao.CurrentPage;
                rtn.TotalPage = t.Dao.TotalPage;
                //结尾sql执行
                sqlparameter.Clear();
                string endsql = t.QueryByPageSQL_End(p, ref sqlparameter);
                if (endsql != "")
                {
                    DBAP dbap = ADBAccess.GetDBParametersDefine();
                    dbap.SQL = endsql;
                    if (sqlparameter.Count > 0)
                    {
                        dbap.SQL_Parameters = sqlparameter;
                    }
                    t.Dao.ExecuteNoQuery(dbap);
                }

                t.Dao.Commit();
                //记录logOperator
                if (p.IsOperatorLog)
                {
                    StringBuilder logmsg = new StringBuilder();
                    logmsg.AppendLine("調用" + t.GetType().FullName + "執行翻頁查詢完畢！");
                    if (presql != "")
                    {
                        logmsg.AppendLine("預執行SQL：" + presql);
                    }
                    logmsg.AppendLine("執行SQL:" + sql);
                    if (endsql != "")
                    {
                        logmsg.AppendLine("收尾SQL：" + endsql);
                    }
                    GlobalCommon.Logger.WriteLog(LoggerLevel.INFO, logmsg.ToString());
                }
                //返回数据
                return rtn;

            }
            catch (Exception ex)
            {
                if (t != null) t.Dao.RollBack();
                GlobalCommon.ExceptionProcessor.DoProcess(ex, _domaintype);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override WebLogicCheckRtn Check<T>(BatchManagerParameter p)
        {
            T t = default(T);
            try
            {
                t = BaseLogic.CreateInstance<T>(this.GetHashCode().ToString());
                return t.Check(p);
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex, _domaintype);
                WebLogicCheckRtn rtn = new WebLogicCheckRtn();
                rtn.IsSuccess = false;
                return rtn;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override CommonLogicReturn GetValue<T>(params object[] p)
        {
            T t = default(T);
            try
            {
                t = BaseLogic.CreateInstance<T>(this.GetHashCode().ToString());
                CommonLogicParameter clp = new CommonLogicParameter();
                for (int i = 0; i < p.Length; i++)
                {
                    clp.SetValue("P" + i, p[i]);
                }
                CommonLogicReturn rtn = t.GetValueBy(clp);
                return rtn;
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }

        public override IsExistsRtn IsExists<T>(BatchManagerParameter p)
        {
            T t = default(T);
            try
            {
                t = BaseLogic.CreateInstance<T>(this.GetHashCode().ToString());
                IsExistsRtn rtn = t.IsExists(p);
                return rtn;
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex, _domaintype);
                return null;
            }
            finally
            {
                ResouceManager.Release(this.GetHashCode().ToString());
            }
        }

        public override IsValidRtn IsValid<T>(BatchManagerParameter p)
        {
            T t = default(T);
            try
            {
                t = BaseLogic.CreateInstance<T>(this.GetHashCode().ToString());
                IsValidRtn rtn = t.IsValid(p);
                return rtn;
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex, _domaintype);
                return null;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }



        public override BatchLogicRtn Process<T>(BatchManagerParameter p)
        {
            T t = default(T);
            try
            {
                t = BaseLogic.CreateInstance<T>(this.GetHashCode().ToString());
                return t.Process(p);
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex, _domaintype);
                BatchLogicRtn rtn = new BatchLogicRtn();
                rtn.IsSuccess = false;
                return rtn;
            }
            finally
            {
                ResouceManager.Release(this);
                ResouceManager.Release(t);
            }
        }
    }
}
